//
//  GHChangePhoneViewController.m
//  GodHorses
//
//  Created by Mac on 2017/11/17.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "GHChangePhoneViewController.h"
#import "STSendButton.h"
@interface GHChangePhoneViewController ()<STSendButtonDlegate>
@property(nonatomic, strong) UITextField                     *phoneTextFiled;
@property(nonatomic, strong) STSendButton                     *sendButton;
@property(nonatomic, strong) UITextField                     *codeTextFiled;
@end

@implementation GHChangePhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"更改手机号";
    [self configSubView];
    // Do any additional setup after loading the view.
}

#pragma mark --subView
- (void)configSubView{
    self.phoneTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 0.70* UIScreenWidth, 45)];
    self.phoneTextFiled.keyboardType = UIKeyboardTypeNumberPad;
    self.phoneTextFiled.placeholder = @"请输入新手机号";
    
    self.sendButton = [[STSendButton alloc] initWithFrame:CGRectMake(0, 0, 80, 45) andWithDuration:60];
    self.sendButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.sendButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.sendButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    self.sendButton.backgroundColor = GH_YellowBackGroundColor;
    self.sendButton.delegate = self;
    self.phoneTextFiled.rightView = self.sendButton;
    self.phoneTextFiled.rightViewMode = UITextFieldViewModeAlways;
    
    

    self.codeTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 0.70* UIScreenWidth, 45)];
    self.codeTextFiled.keyboardType = UIKeyboardTypeNumberPad;
    self.codeTextFiled.placeholder = @"请输入验证码";
    
    //下一步
    __weak typeof(self) weakSelf =  self;
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    footer.backgroundColor = self.tableView.backgroundColor;
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(30, 0, UIScreenWidth - 60, 44)
                                                        title:@"下一步"
                                                   titleColor:GH_YellowBackGroundColor
                                                    titleFont:18
                                                 cornerRadius:5
                                              backgroundColor:GH_BlackBackGroundColor
                                              backgroundImage:nil
                                                        image:nil];
    [confimButton setClicAction:^(UIButton *sender) {
        if (!weakSelf.phoneTextFiled.text.length) {
            [SVProgressHUD showErrorWithStatus:@"请输入手机号"];
            return ;
        }
        if (!weakSelf.codeTextFiled.text.length) {
            [SVProgressHUD showErrorWithStatus:@"请输入验证码"];
            return ;
        }
        [weakSelf sendCheckCodeAndPhoneRequest];
    }];
    confimButton.st_centerY = 50;
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [footer addSubview:confimButton];
    self.tableView.tableFooterView = footer;
    
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 45;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    cell.textLabel.textColor = UIColorFromRGBA(0x333333);
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
    cell.detailTextLabel.textColor = UIColorFromRGBA(0x999999);
    
    if (indexPath.row == 0) {
        cell.textLabel.text = @"新手机号";
        cell.accessoryView = self.phoneTextFiled;
    }
    if (indexPath.row == 1) {
         cell.textLabel.text = @"验证码";
         cell.accessoryView = self.codeTextFiled;
    }

    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --STSendButtonDlegate
- (BOOL)STSendButtonWillClic:(UIButton *)button{
    
    return YES;
}
- (void)STSendButtonDidClic:(UIButton*)button isFirstClic:(BOOL)isFirstClic duration:(NSInteger)duration{
    if (isFirstClic) {
        [self sendcodeRequest];
    }
}

- (void)STSendButtonDidCountdown:(UIButton*)button duration:(NSInteger)duration{
    [self.sendButton setTitle:[NSString stringWithFormat:@"%ld秒",duration] forState:UIControlStateNormal];
}
- (void)STSendButtonTimeEnded:(UIButton*)button{
    [self.sendButton timerEnd];
    [self.sendButton setTitle:@"获取验证码" forState:UIControlStateNormal];
}
#pragma mark --NetWork Method
- (void)sendcodeRequest{
    
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if (self.phoneTextFiled.text.length) {
        [dic setObject:self.phoneTextFiled.text forKey:@"mobile"];
    }
    [SVProgressHUD showWithStatus:@"正在发送"];
    [[STNetWrokManger defaultClient] requestWithPath:@"http://test.smqyp.com/Api/User/sendSms"
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showSuccessWithStatus:@"验证码已成功发送"];
                                                 [self.sendButton timerBegin];
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [self.sendButton timerEnd];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
- (void)sendCheckCodeAndPhoneRequest{
    [SVProgressHUD showWithStatus:@"验证中"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([GHUserManger defult].userID.length) {
        [dic setObject:[GHUserManger defult].userID forKey:@"uid"];
    }
    if ([GHUserManger defult].token.length) {
        [dic setObject:[GHUserManger defult].token forKey:@"token"];
    }
    if (self.phoneTextFiled.text.length) {
        [dic setObject:self.phoneTextFiled.text forKey:@"mobile"];
    }
    if (self.codeTextFiled.text.length) {
        [dic setObject:self.phoneTextFiled.text forKey:@"code"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:@"http://test.smqyp.com/Api/system/check_code"
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 //去下一步
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:@"验证码或者原手机号错误"];
                                             }];
}
@end
